layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
        
    //用户列表
    var tableIns = table.render({
        elem: '#investList',
        url : 'http://127.0.0.1:88/index.php/invest/invest/investList?id='+localStorage.login_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "investListTable",
        cols : [[
            {field: 'invest_id', title: '流水号' , align:"center"},
            {field: 'user_name', title: '用户名', width:100, align:"center"},
            {field: 'count_type', title: '支付平台', width:100, align:"center"},
            {field: 'count_num', title: '充值账户', width:200, align:'center'},
            {field: 'invest_money', title: '充值金额', width:100, align:'center'},
            {field: 'invest_addTime', title: '充值时间', align:'center'},
           	{field: 'invest_status', title: '充值状态', align:'center',templet:function(d){
            	if(d.invest_status==0){
            		return '<font color="red">未知</font>';
            	}else{
            		if (d.invest_status=="充值成功") {
            			return '<font color="blue">'+d.invest_status+'</font>';
            		} else{
            			return '<font color="red">'+d.invest_status+'</font>'
            					+'<button type="button" class="layui-btn layui-btn-xs layui-btn-warm" lay-event="reason">查看原因</button>';
            		}
            		
            	}
                
            }},
            {title: '操作', templet:'#investListBar', align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
    	var type = $(this).data('type');
    	//console.log(type);
        if($(".searchVal").val() != ''){
            table.reload("investListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'post',
                url:'http://127.0.0.1:88/index.php/invest/invest/investSearch',
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    user_name:localStorage.login_name
                },
                contentType: 'application/json',
                done: function(res, curr, count){
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
              }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addinvest(edit){
        var index = layui.layer.open({
            title : "编辑",
            type : 2,
            area:["550px","400px"],
            content : "investAdd.html",
            success : function(layero, index){
                /*var body = layui.layer.getChildFrame('body', index);
                body.find(".invest_name").val(edit.invest_name);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();*/
                /*if(edit){
                    body.find(".userName").val(edit.userName);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                }*/
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
    	if (localStorage.login_role==1) {
    		layer.msg("无操作权限");
    	} else{
    		addinvest();
    	}
        
    })
	//刷新invest_refresh
	$(".invest_refresh").click(function(){
        tableIns.reload();
    })
	
    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('investListTable'),
            data = checkStatus.data,
            invest_id = [];
            
        if(data.length > 0) {
            for (var i in data) {
                invest_id.push(data[i].invest_id);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("http://127.0.0.1:88/index.php/invest/invest/delMoreinvest",{
                    invest_id : invest_id  //将需要删除的invest_name作为参数传入
                },function(data){
                tableIns.reload();
                layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })

    //列表操作
    table.on('tool(investList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'check'){ //编辑
        	console.log(data);
        	if (data.invest_status=="充值成功") {
        		layer.msg("已通过，不能更改");
        	} else{
        		var index = layui.layer.open({
		            title : "审核",
		            type : 2,
		            area:["450px","400px"],
		            content : "investCheck.html",
		            success : function(layero, index){
		            	//console.log(data);
		                var body = layui.layer.getChildFrame('body', index);
		               		body.find("#invest_name").val(data.user_name);
			                body.find(".invest_type").val(data.count_type);
			                body.find(".invest_num").val(data.count_num);
			                body.find(".invest_money").val(data.invest_money);
			                body.find("#picture").attr("src",data.invest_img_url);
			                body.find(".invest_addTime").val(data.invest_addTime);
			               	body.find("#invest_id").val(data.invest_id);
			               	form.render();
			                /*if(edit){
			                    body.find(".userName").val(edit.userName);  //登录名
			                    body.find(".userEmail").val(edit.userEmail);  //邮箱
			                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
			                    body.find(".userGrade").val(edit.userGrade);  //会员等级
			                    body.find(".userStatus").val(edit.userStatus);    //用户状态
			                    body.find(".userDesc").text(edit.userDesc);    //用户简介
			                    form.render();
			                }*/
			            setTimeout(function(){
			                layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
			                    tips: 3
			                });
			            },500)
		            }
		        })
        	}
            
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
        	if (localStorage.login_role==1) {
    		layer.msg("无操作权限");
    	} else{
    		layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                $.get("http://127.0.0.1:88/index.php/invest/invest/delinvest",{
                    id : data.invest_id  //将需要删除的newsId作为参数传入
                },function(data){
                	if(data.code==200){
                		layer.msg(data.msg);
                		setTimeout(function(){
                			tableIns.reload();
                    		layer.close(index);
                		},1000);
                	}else{
                		layer.msg(data.msg);
                	}
                })
            });
    	}
        }else if(layEvent === 'detail'){
        	var index = layui.layer.open({
	            title : "店铺详情",
	            type : 2,
	            area:["450px","420px"],
	            content : "investDetail.html",
	            success : function(layero, index){
	            	var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/invest/invest/detailinvest",{
	                    id : data.invest_name  //将需要删除的newsId作为参数传入
	                },function(data){
	                	
	                	body.find(".invest_name").val(data.detail[0].invest_name);
	                    body.find(".invest_wangwang").val(data.detail[0].invest_name);
	                    body.find(".invest_type").val(data.detail[0].invest_name);
	                    body.find(".invest_img").attr("src",data.detail[0].invest_img_url);
	                    form.render();
	                })
	                /*var body = layui.layer.getChildFrame('body', index);
	                
	                    body.find(".invest_name").val("detail.userName");  //登录名
	                    body.find(".userEmail").val(edit.userEmail);  //邮箱
	                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
	                    body.find(".userGrade").val(edit.userGrade);  //会员等级
	                    body.find(".userStatus").val(edit.userStatus);    //用户状态
	                    body.find(".userDesc").text(edit.userDesc);    //用户简介
	                    form.render();*/
	                
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
	        })
        }else if(layEvent === 'reason'){
        	var index = layui.layer.open({
	            type: 1
	            ,title:'失败原因'
		        ,content: '<div style="padding: 20px 100px;">'+data.check_desc+'</div>'
		        ,shade: 0 //不显示遮罩
		        ,success: function(){
		          /*setTimeout(function(){
	                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)*/
		        }
        	})
        }
    });

})